﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Tree.ConsoleApp
{
    public class BiTree<T>
    {
        private Node<T> head { get; set; }

        /// <summary>
        /// 头节点
        /// </summary>
        public Node<T> Head
        {
            get { return head; }
            set { head = value; }
        }
        public BiTree()
        {
            head = null;
        }
        public BiTree(T val)
        {
            Node<T> p = new Node<T>(val);
            head = p;
        }
        public BiTree(T val, Node<T> lp, Node<T> rp)
        {
            Node<T> p = new Node<T>(val, lp, rp);
            head = p;
        }

        /// <summary>
        /// 判断头节点是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
            if (head == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 获取根节点
        /// </summary>
        /// <returns></returns>
        public Node<T> Root()
        {
            return head;
        }
        /// <summary>
        /// 获取节点左子树节点
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public Node<T> GetLChild(Node<T> p)
        {
            return p.LChild;
        }
        /// <summary>
        /// 获取节点右子树节点
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public Node<T> GetRChild(Node<T> p)
        {
            return p.RChild;
        }
        /// <summary>
        /// 将节点p的左子树插入值为val的新节点，原来左子树成为新节点的左子树
        /// </summary>
        /// <param name="val"></param>
        /// <param name="p"></param>
        public void InsertL(T val, Node<T> p)
        {
            Node<T> tmp = new Node<T>(val);
            tmp.LChild = p.LChild;
            p.LChild = tmp;
        }
        /// <summary>
        /// 将节点p的右子树插入值为val的新节点，原来右子树成为新节点的右子树
        /// </summary>
        /// <param name="val"></param>
        /// <param name="p"></param>
        public void InsertR(T val, Node<T> p)
        {
            Node<T> tmp = new Node<T>(val);
            tmp.RChild = p.RChild;
            p.RChild = tmp;
        }
        /// <summary>
        /// 若P非空，删除p的左子树
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public Node<T> DeleteL(Node<T> p)
        {
            if (p == null || p.LChild == null)
            {
                return null;
            }
            Node<T> tmp = p.LChild;
            p.LChild = null;
            return tmp;
        }
        /// <summary>
        /// 若P非空，删除p的右子树
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public Node<T> DeleteR(Node<T> p)
        {
            if (p == null || p.RChild == null)
            {
                return null;
            }
            Node<T> tmp = p.RChild;
            p.RChild = null;
            return tmp;
        }
        /// <summary>
        /// 判断是否为叶子节点
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public bool IsLeaf(Node<T> p)
        {
            if (p != null && p.LChild != null & p.RChild != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
